<template>
  <el-dialog v-el-drag-dialog :title="!form.id?'新建库存':'编辑库存'" :visible.sync="visible" :append-to-body="appendToBody">
    <el-form ref="wareForm" :model="form" :rules="rules" label-width="120px" :disabled="!hasLoaded">
      <el-card>
        <el-form-item hidden>
          <el-input v-model="form.id" hidden/>
          <el-input v-model="form.skuId" hidden/>
        </el-form-item>
        <el-form-item label="商品名称" prop="skuName">
          <el-input v-model="form.skuName" readonly />
        </el-form-item>
        <el-form-item label="仓库" prop="wareId">
          <el-select v-model="form.wareId" filterable clearable>
            <el-option
              v-for="item in wareData"
              :key="'ware_' + item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库存" prop="stock">
          <el-input-number v-model="form.stock" :min="0" />
        </el-form-item>
        <el-form-item label="锁定库存" prop="stockLocked">
          <el-input-number v-model="form.stockLocked" :min="0" :max="form.stock" />
        </el-form-item>
      </el-card>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button v-if="hasLoaded" type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { wareList, wareSkuLoad, wareSkuSave } from '@/api/mall/ware/ware'

export default {
  name: 'WareSkuFormDialog',
  data() {
    return {
      visible: false,
      appendToBody: true,
      hasLoaded: false,
      form: {
        id: undefined,
        skuId: undefined,
        wareId: undefined,
        skuName: undefined,
        stock: 0,
        stockLocked: 0
      },
      rules: {
        wareId: [{ required: true, trigger: 'blur', message: '请选择仓库' }]
      },
      wareData: []
    }
  },
  methods: {
    init(row) {
      this.hasLoaded = false
      this.form = this.$api.objectUpdate(this.form, row)
      this.visible = true
      this.$nextTick(() => {
        this.getWareData()
        this.loadForm()
      })
    },
    getWareData() {
      wareList().then(response => {
        this.wareData = response
      }).catch(() => {})
    },
    loadForm() {
      wareSkuLoad({ id: this.form.id, skuId: this.form.skuId }).then(response => {
        this.form = this.$api.objectUpdate(this.form, response.param)
        // 判断当前用户权限
        this.hasLoaded = this.$perm.hasPermissionOr(['mall:ware:edit'])
      }).catch(() => {
      })
    },
    onSubmit() {
      this.$refs.wareForm.validate(valid => {
        if (valid) {
          wareSkuSave(this.form).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$emit('confirmForm')
            this.onClose()
          }).catch(() => {
          })
        }
      })
    },
    onClose() {
      this.$refs.wareForm.resetFields()
      this.visible = false
    }
  }
}
</script>
